package com.ccys.cms.service.administrator.impl;


import com.ccys.cms.service.administrator.TblGroupWebService;
import com.ccys.common.util.StringUtil;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.cache.UserUtil;
import com.ccys.db.dto.administrator.TblGroupDTO;
import com.ccys.db.entity.TblGroup;
import com.ccys.db.mapper.TblGroupMapper;
import com.ccys.db.page.PageDto;
import com.ccys.db.page.PageUtils;
import com.ccys.db.rely.service.impl.BaseServiceImpl;
import com.ccys.db.service.BaseTblAuditTrailWebService;
import com.ccys.db.vo.administrator.CompanySelectListWebVO;
import com.ccys.db.vo.administrator.GroupSelectListWebVO;
import com.ccys.db.vo.administrator.TblGroupWebVO;

import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Iterator;
import java.util.List;


/**
 * 集团管理
 */
@Service
@Slf4j
public class TblGroupWebServiceImpl extends BaseServiceImpl<TblGroup> implements TblGroupWebService {

    @Autowired(required = false)
    private TblGroupMapper tblGroupMapper;
    @Autowired(required = false)
    private BaseTblAuditTrailWebService baseTblAuditTrailWebService;

    @Override
    public ResponseResult listObjectsCms(HttpServletRequest request, HttpServletResponse response, TblGroupDTO dto) {
        PageUtils.startPage(request);
        List<TblGroupWebVO> list = this.tblGroupMapper.listObjectsCms(dto);
        PageInfo pageInfo = new PageInfo<>(list);
        return ResponseResult.success(pageInfo);
    }


    @Override
    public ResponseResult change(HttpServletRequest request, HttpServletResponse response, TblGroup entity) {
        if (StringUtils.isBlank(entity.getId())) { // 新增
            if(entity.getGroupStatus() == null) {
                entity.setGroupStatus(1);
            }
            this.saveEntity(entity);
        } else { // 编辑
            this.updateEntityById(entity);
            if(entity.getGroupStatus() != null) {
                this.baseTblAuditTrailWebService.saveStatusLog(entity,entity.getGroupStatus());
            }
        }
        return ResponseResult.success();
    }

    @Override
    public ResponseResult delete(HttpServletRequest request, HttpServletResponse response, TblGroup dto) {
        TblGroup entity = new TblGroup();
        entity.setId(dto.getId());
        entity.setDelFlag(1);
        this.updateEntityById(entity);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult groupSelectList(TblGroupDTO dto) {
        //PageDto.startPage();
        String curLoginUserGroupId = UserUtil.getCurLoginUserGroupId();
        if(StringUtil.isNotEmpty(curLoginUserGroupId)) {
            //2024-02-06前端要求过滤当前登录人所属的Group数据
            dto.setFilterateGroupId(curLoginUserGroupId);
        }
        List<GroupSelectListWebVO> list = this.tblGroupMapper.groupSelectList(dto);
        //PageInfo pageInfo = new PageInfo<>(list);
        return ResponseResult.success(list);
    }


}